package com.guapi.dao;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.Page;
import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 瓜皮哦
 */
@Data
public class ResponseData {
    // 返回状态编码
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String code;

    // 返回信息
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String message;

    //数据
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private List<?> rows;

    //数据
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Map<String, Object> data;

    //成功标识
    private boolean success = true;

    //总数
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long total;


    public ResponseData(boolean success) {
        setSuccess(success);
    }

    public ResponseData(List<?> list) {
        this(true);
        setRows(list);
    }

    public ResponseData() {
        this.data = new HashMap<>();
    }

    public boolean isSuccess() {
        return success;
    }


    public void setRows(List<?> rows) {
        this.rows = rows;
        if (rows instanceof Page) {
            setTotal(((Page<?>) rows).getTotal());
        } else {
            setTotal((long) rows.size());
        }
    }

    public void setSuccess(boolean success) {
        if (success) {
            this.setCode("200");
        }
        this.success = success;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.setSuccess(true);
        this.data = data;
    }

    public Map<String, Object> setData(String key, Object value) {
        this.data.put(key, value);
        return data;
    }
}
